local function stain_formspec(pos)
   local meta = minetest.get_meta(pos)
   local chitin_level = tonumber(meta:get_string('chitin_level'))
   local grounds_level = tonumber(meta:get_string('grounds_level'))
   local water_level = tonumber(meta:get_string('water_level'))
   formspec =
   'size[8,8.5]'..
   'list[current_name;chitin;.5,0;1,1;]'..
   'label[1.5,0.25;Input chitin]'..
   'list[current_name;grounds;.5,1;1,1;]'..
   'label[1.5,1.25;Input coffee grounds]'..
   'list[current_name;water;.5,2;1,1;]'..
   'label[1.5,2.25;Input water]'..
   'list[current_name;brush;.5,3;1,1;]'..
   'label[1.5,3.25;Input brush]'..
   'list[current_name;output;4.5,3;1,1;]'..
   'image[3,.25;3,.5;station_stain_bg.png^[lowpart:'..(chitin_level*5)..':station_stain_fg.png^[transformR270]'..
   'image[3,1.25;3,.5;station_stain_bg.png^[lowpart:'..(grounds_level*5)..':station_stain_fg.png^[transformR270]'..
   'image[3,2.25;3,.5;station_stain_bg.png^[lowpart:'..(water_level*5)..':station_stain_fg.png^[transformR270]'..
   'item_image_button[6,0;1,1;furniture:stain_brush1;brush1; ]'..
   'item_image_button[6,1;1,1;furniture:stain_brush2;brush2; ]'..
   'item_image_button[6,2;1,1;furniture:stain_brush3;brush3; ]'..
   'item_image_button[7,0;1,1;furniture:stain_brush4;brush4; ]'..
   'item_image_button[7,1;1,1;furniture:stain_brush5;brush5; ]'..
   'item_image_button[7,2;1,1;furniture:stain_brush6;brush6; ]'..
   'item_image_button[7,3;1,1;furniture:stain_brush7;brush7; ]'..
   'list[current_player;main;0,4.5;8,4;]'..
   'listring[context;output]'..
   'listring[current_player;main]'
   return formspec
end

minetest.register_node('stations:stain', {
   description = 'Stain Station',
   drawtype = 'mesh',
   mesh = 'stations_stain.obj',
   tiles = {'stations_stain.png'},
   sounds = default.node_sound_wood_defaults(),
   paramtype2 = 'facedir',
   paramtype = 'light',
   selection_box = {
      type = 'fixed',
      fixed = {-.5, -.5, -.5, .5, .5, .5}},
   collision_box = {
      type = 'fixed',
      fixed = {-.5, -.5, -.5, .5, .5, .5}},
   groups = {oddly_breakable_by_hand = 1, choppy=3},
   on_construct = function(pos)
      local meta = minetest.get_meta(pos)
      local inv = meta:get_inventory()
      inv:set_size('main', 8*4)
      inv:set_size('chitin', 1)
      inv:set_size('grounds', 1)
      inv:set_size('water', 1)
      inv:set_size('brush', 1)
      inv:set_size('output', 1)
      meta:set_string('chitin_level', 0)
      meta:set_string('grounds_level', 0)
      meta:set_string('water_level', 0)
      meta:set_string('infotext', 'Stain Station')
      meta:set_string('formspec', stain_formspec(pos))
   end,
   can_dig = function(pos,player)
      local meta = minetest.get_meta(pos);
      local inv = meta:get_inventory()
      if inv:is_empty('chitin') and inv:is_empty('grounds') and inv:is_empty('water')
      and inv:is_empty('brush') and inv:is_empty('output') then
         return true
      else
         return false
      end
   end,
   allow_metadata_inventory_put = function(pos, listname, index, stack, player)
      local input = stack:get_name()
      if listname == 'chitin' then
         if input == 'stations:chitin' then
            return 1
         else
            return 0
         end
      elseif listname == 'grounds' then
         if input == 'stations:coffee_grounds' then
            return 1
         else
            return 0
         end
      elseif listname == 'water' then
         if input == 'bucket:bucket_river_water' or input == 'bucket:bucket_water'
         or input == 'earthbuild:clay_pot_river_water' or input == 'earthbuild:clay_pot_water' then
            return 1
         else
            return 0
         end
      elseif listname == 'brush' then
         if minetest.get_item_group(input, 'stain_brush') > 0 then
            return 99
         else
            return 0
         end
      elseif listname == 'output' then
         return 0
      end
   end,
   on_metadata_inventory_put = function(pos, listname, index, stack, player)
      local meta = minetest.get_meta(pos)
      local input = stack:get_name()
      local inv = meta:get_inventory()
      local chitin_level = tonumber(meta:get_string('chitin_level'))
      local grounds_level = tonumber(meta:get_string('grounds_level'))
      local water_level = tonumber(meta:get_string('water_level'))
      if listname == 'grounds' then
         if grounds_level + 2 <= 20 then
            meta:set_string('grounds_level', grounds_level+2)
            inv:set_stack('grounds', 1, '')
         end
      elseif listname == 'water' then
         if water_level + 10 <= 20 then
            meta:set_string('water_level', water_level+10)
            local vessel = string.sub(input, 1,3)
            if vessel == 'ear' then
               inv:set_stack('water', 1, 'earthbuild:clay_pot')
            elseif vessel == 'buc' then
               inv:set_stack('water', 1, 'bucket:bucket_empty')
            end
         end
      elseif listname == 'chitin' then
         if chitin_level + 2 <= 20 then
            meta:set_string('chitin_level', chitin_level+2)
            inv:set_stack('chitin', 1, '')
         end
      end
      meta:set_string('formspec', stain_formspec(pos))
   end,
   on_receive_fields = function(pos, formname, fields, sender)
      local meta = minetest.get_meta(pos)
      local inv = meta:get_inventory(pos)
      local chitin_level = tonumber(meta:get_string('chitin_level'))
      local grounds_level = tonumber(meta:get_string('grounds_level'))
      local water_level = tonumber(meta:get_string('water_level'))
      local brush = inv:get_stack('brush', 1)
      local brush_count = brush:get_count()
      local make_okay = false
      if chitin_level >= 1 and water_level >= 1 and brush_count >= 1 then
         if fields['brush1'] then
            if grounds_level >= 1 then
               inv:set_stack('output', 1, 'furniture:stain_brush1')
               meta:set_string('grounds_level', grounds_level-1)
               make_okay = true
            end
         elseif fields['brush2'] then
            if grounds_level >= 2 then
               inv:set_stack('output', 1, 'furniture:stain_brush2')
               meta:set_string('grounds_level', grounds_level-2)
               make_okay = true
            end
         elseif fields['brush3'] then
            if grounds_level >= 3 then
               inv:set_stack('output', 1, 'furniture:stain_brush3')
               meta:set_string('grounds_level', grounds_level-3)
               make_okay = true
            end
         elseif fields['brush4'] then
            if grounds_level >= 4 then
               inv:set_stack('output', 1, 'furniture:stain_brush4')
               meta:set_string('grounds_level', grounds_level-4)
               make_okay = true
            end
         elseif fields['brush5'] then
            if grounds_level >= 5 then
               inv:set_stack('output', 1, 'furniture:stain_brush5')
               meta:set_string('grounds_level', grounds_level-5)
               make_okay = true
            end
         elseif fields['brush6'] then
            if grounds_level >= 6 then
               inv:set_stack('output', 1, 'furniture:stain_brush6')
               meta:set_string('grounds_level', grounds_level-6)
               make_okay = true
            end
         elseif fields['brush7'] then
            if grounds_level >= 7 then
               inv:set_stack('output', 1, 'furniture:stain_brush7')
               meta:set_string('grounds_level', grounds_level-7)
               make_okay = true
            end
         end
         if make_okay == true then
            meta:set_string('chitin_level', chitin_level-1)
            meta:set_string('water_level', water_level-1)
            brush:take_item()
            inv:set_stack('brush',1,brush)
            meta:set_string('formspec', stain_formspec(pos))
         end
      end
   end,
})

minetest.register_node('stations:stain_locked', {
   description = 'Stain Station (locked)',
   drawtype = 'mesh',
   mesh = 'stations_stain.obj',
   tiles = {'stations_stain.png'},
   sounds = default.node_sound_wood_defaults(),
   paramtype2 = 'facedir',
   paramtype = 'light',
   selection_box = {
      type = 'fixed',
      fixed = {-.5, -.5, -.5, .5, .5, .5}},
   collision_box = {
      type = 'fixed',
      fixed = {-.5, -.5, -.5, .5, .5, .5}},
   groups = {oddly_breakable_by_hand = 1, choppy=3},
   on_construct = function(pos)
      local meta = minetest.get_meta(pos)
      local inv = meta:get_inventory()
      inv:set_size('main', 8*4)
      inv:set_size('chitin', 1)
      inv:set_size('grounds', 1)
      inv:set_size('water', 1)
      inv:set_size('brush', 1)
      inv:set_size('output', 1)
      meta:set_string('chitin_level', 0)
      meta:set_string('grounds_level', 0)
      meta:set_string('water_level', 0)
      meta:set_string('infotext', 'Stain Station (locked)')
      meta:set_string('formspec', stain_formspec(pos))
   end,
   can_dig = function(pos,player)
      local meta = minetest.get_meta(pos);
      local inv = meta:get_inventory()
      if inv:is_empty('chitin') and inv:is_empty('grounds') and inv:is_empty('water')
      and inv:is_empty('brush') and inv:is_empty('output') then
         return true
      else
         return false
      end
   end,
   allow_metadata_inventory_put = function(pos, listname, index, stack, player)
      local player_name = player:get_player_name()
      if minetest.is_protected(pos, player_name) and not minetest.check_player_privs(player, 'protection_bypass') then
         return 0
      else
         local input = stack:get_name()
         if listname == 'chitin' then
            if input == 'stations:chitin' then
               return 1
            else
               return 0
            end
         elseif listname == 'grounds' then
            if input == 'stations:coffee_grounds' then
               return 1
            else
               return 0
            end
         elseif listname == 'water' then
            if input == 'bucket:bucket_river_water' or input == 'bucket:bucket_water'
            or input == 'earthbuild:clay_pot_river_water' or input == 'earthbuild:clay_pot_water' then
               return 1
            else
               return 0
            end
         elseif listname == 'brush' then
            if minetest.get_item_group(input, 'stain_brush') > 0 then
               return 99
            else
               return 0
            end
         elseif listname == 'output' then
            return 0
         end
      end
   end,
   on_metadata_inventory_put = function(pos, listname, index, stack, player)
      local meta = minetest.get_meta(pos)
      local input = stack:get_name()
      local inv = meta:get_inventory()
      local chitin_level = tonumber(meta:get_string('chitin_level'))
      local grounds_level = tonumber(meta:get_string('grounds_level'))
      local water_level = tonumber(meta:get_string('water_level'))
      if listname == 'grounds' then
         if grounds_level + 2 <= 20 then
            meta:set_string('grounds_level', grounds_level+2)
            inv:set_stack('grounds', 1, '')
         end
      elseif listname == 'water' then
         if water_level + 10 <= 20 then
            meta:set_string('water_level', water_level+10)
            local vessel = string.sub(input, 1,3)
            if vessel == 'ear' then
               inv:set_stack('water', 1, 'earthbuild:clay_pot')
            elseif vessel == 'buc' then
               inv:set_stack('water', 1, 'bucket:bucket_empty')
            end
         end
      elseif listname == 'chitin' then
         if chitin_level + 2 <= 20 then
            meta:set_string('chitin_level', chitin_level+2)
            inv:set_stack('chitin', 1, '')
         end
      end
      meta:set_string('formspec', stain_formspec(pos))
   end,
   on_receive_fields = function(pos, formname, fields, sender)
      local player_name = sender:get_player_name()
      if minetest.is_protected(pos, player_name) and not minetest.check_player_privs(player, 'protection_bypass') then
         minetest.chat_send_player(sender, 'You don\'t have access to this station.')
      else
         local meta = minetest.get_meta(pos)
         local inv = meta:get_inventory(pos)
         local chitin_level = tonumber(meta:get_string('chitin_level'))
         local grounds_level = tonumber(meta:get_string('grounds_level'))
         local water_level = tonumber(meta:get_string('water_level'))
         local brush = inv:get_stack('brush', 1)
         local brush_count = brush:get_count()
         local make_okay = false
         if chitin_level >= 1 and water_level >= 1 and brush_count >= 1 then
            if fields['brush1'] then
               if grounds_level >= 1 then
                  inv:set_stack('output', 1, 'furniture:stain_brush1')
                  meta:set_string('grounds_level', grounds_level-1)
                  make_okay = true
               end
            elseif fields['brush2'] then
               if grounds_level >= 2 then
                  inv:set_stack('output', 1, 'furniture:stain_brush2')
                  meta:set_string('grounds_level', grounds_level-2)
                  make_okay = true
               end
            elseif fields['brush3'] then
               if grounds_level >= 3 then
                  inv:set_stack('output', 1, 'furniture:stain_brush3')
                  meta:set_string('grounds_level', grounds_level-3)
                  make_okay = true
               end
            elseif fields['brush4'] then
               if grounds_level >= 4 then
                  inv:set_stack('output', 1, 'furniture:stain_brush4')
                  meta:set_string('grounds_level', grounds_level-4)
                  make_okay = true
               end
            elseif fields['brush5'] then
               if grounds_level >= 5 then
                  inv:set_stack('output', 1, 'furniture:stain_brush5')
                  meta:set_string('grounds_level', grounds_level-5)
                  make_okay = true
               end
            elseif fields['brush6'] then
               if grounds_level >= 6 then
                  inv:set_stack('output', 1, 'furniture:stain_brush6')
                  meta:set_string('grounds_level', grounds_level-6)
                  make_okay = true
               end
            elseif fields['brush7'] then
               if grounds_level >= 7 then
                  inv:set_stack('output', 1, 'furniture:stain_brush7')
                  meta:set_string('grounds_level', grounds_level-7)
                  make_okay = true
               end
            end
            if make_okay == true then
               meta:set_string('chitin_level', chitin_level-1)
               meta:set_string('water_level', water_level-1)
               brush:take_item()
               inv:set_stack('brush',1,brush)
               meta:set_string('formspec', stain_formspec(pos))
            end
         end
      end
   end,
})
